package com.example.demo;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.*;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebServlet(name = "helloServlet", value = "/hello-servlet")
public class HelloServlet extends HttpServlet {
    private String message;

    public void init() {
        message = "Hello World!";
    }

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html");
        doPost(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("application/json;charset=utf-8");

        String name = request.getParameter("username");
        System.out.println(name);
        PrintWriter out = response.getWriter();
        Map<String, Object> map = new HashMap<>();
        if ("wan".equals(name)) {
            map.put("userExsit", true);
            map.put("msg", "用户名从在");
        } else {
            map.put("userExsit", false);
            map.put("msg", "用户名可用");
        }
        //转化json
        ObjectMapper mapper = new ObjectMapper();
        mapper.writeValue(response.getWriter(), map);
    }

    public void destroy() {
    }
}